package com.sskj.base.utils;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sskj.base.R;
import com.sskj.base.R2;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TipDialog extends AlertDialog {

    @BindView(R2.id.title_text)
    TextView titleText;
    @BindView(R2.id.content_text)
    TextView contentText;
    @BindView(R2.id.cancel_btn)
    TextView cancelBtn;
    @BindView(R2.id.confirm_btn)
    TextView confirmBtn;
    @BindView(R2.id.check_show)
    public CheckBox checkShow;


    private OnClickListener listener;

    private View mView;

    public TipDialog(@NonNull Context context) {
        super(context);
        mView = LayoutInflater.from(getContext()).inflate(R.layout.base_tip_dialog, null);
        setView(mView);
        setCancelable(false);
        ButterKnife.bind(this, mView);
        initEvent();
    }


    public TipDialog setTitle(String title) {
        titleText.setText(title);
        return this;
    }

    public TipDialog setContent(String title) {
        contentText.setText(title);
        return this;
    }

    public TipDialog setConfirmButtonText(String title) {
        confirmBtn.setText(title);
        return this;
    }

    public TipDialog setCancelButtonText(String title) {
        cancelBtn.setText(title);
        return this;
    }

    public TipDialog setCancelButtonVisible(int visible) {
        cancelBtn.setVisibility(visible);
        return this;
    }

    public TipDialog setTitleVisible(int visible) {
        titleText.setVisibility(visible);
        return this;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void initEvent() {
        cancelBtn.setOnClickListener(v -> dismiss());
        confirmBtn.setOnClickListener(v -> {
            if (listener != null) {
                dismiss();
                listener.onClick(this);
            }
        });
    }

    public interface OnClickListener {
        void onClick(TipDialog dialog);
    }

    public TipDialog setListener(OnClickListener listener) {
        this.listener = listener;
        return this;
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = (int) (ScreenUtil.getScreenWidth(getContext()) * 0.9);
        getWindow().setAttributes(layoutParams);
    }
}

